﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Infrastructure;
using MiniFox.Platform.OAuth2.Models;
using MiniFox.Platform.OAuth2.Web.Filters;
using MiniFox.Platform.Web.Filters;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web;
using MiniFox.Web.Infrastructure;
using System.ComponentModel.DataAnnotations;

namespace MiniFox.Platform.OAuth2.Web
{
    /// <summary>
    /// 
    /// </summary>
    [AllowAnonymous, NoCache, PassportFilter]
    public class AuthorizationController : OAuth2ControllerBase<IAuthorizationService>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="redirectUri"></param>
        /// <param name="responseType"></param>
        /// <param name="context"></param>
        /// <param name="scope"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Connect([FromQuery(Name = OpenAuthKey.AUTH_APP_ID), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), MaxLength(32)] string appId,
            [FromQuery(Name = OpenAuthKey.AUTH_REDIRECT_URI), RegularExpression(StringExtensions.PATTERN_URI, ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_InvalidURI"), MaxLength(2048)] string redirectUri,
            [FromQuery(Name = OpenAuthKey.AUTH_RESPONSE_TYPE), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] AuthResponseType responseType,
            [FromQuery(Name = OpenAuthKey.AUTH_CLIENT_CONTEXT), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), MaxLength(64)] string context,
            [FromQuery(Name = OpenAuthKey.AUTH_SCOPE)] AuthScope scope)
        {
            var response = this.Component.CreateAuthorizeData(appId, redirectUri, context, scope);
            return this.ResponseResult(response);
        }

        #region 注销

        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SignOut([FromQuery(Name = OpenAuthKey.AUTH_REDIRECT_URI)] string redirectUri)
        {
            var response = this.Component.SignOut(redirectUri);
            return this.ResponseResult(response);
        }

        #endregion

        #region 修改密码

        /// <summary>
        /// 
        /// </summary>
        /// <param name="changePassword"></param>
        /// <returns></returns>
        [HttpPost, Verify]
        public Response ChangePassword([FromBody] ChangePassword changePassword)
        {
            Response response = new();
            if (this.Component.ChangePassword(changePassword.Password, changePassword.NewPassword))
            {
                response.Message = Resources.ResMessage.MSG_ChangePwdSuccessful;
            }
            else
            {
                response.Code = Resources.ResErrorCode.ERR_ChangePwdFail;
                response.Message = Resources.ResMessage.ERR_ChangePwdFail;
            }
            return response;
        }
        #endregion
    }
}
